package org.deschutter.scans;

import org.deschutter.URLReader;
import org.deschutter.constants.Config;
import org.deschutter.exceptions.AddScanException;
import org.deschutter.hibernate.Planets;
import org.deschutter.hibernate.ScanType;
import org.deschutter.hibernate.Scans;
import org.deschutter.hibernate.Stats;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.util.List;

/**
 * User: Berten De Schutter
 * Date: 01-Jul-2009
 * Time: 11:21:45
 */
public class Scan implements Config {
    private String ID;
    private String type;
    private BigDecimal tick;
    private String scanpage;
    private String x, y, z;
    private BigDecimal userID;
    private Scans hbscan;
    private Session session;

    Scan(String ID, String type, BigDecimal tick, String x, String y, String z, String scanpage, BigDecimal userID, Session session) {
        this.ID = ID;
        this.type = type;
        this.tick = tick;
        this.x = x;
        this.y = y;
        this.z = z;
        this.session = session;
        if (scanpage == null) {
            try {
                this.scanpage = URLReader.get(PLANETARION_URL + SCAN_URL + "scan_id=" + ID);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        } else {
            this.scanpage = scanpage;
        }

        this.userID = userID;


        try {
            addScanBase();
            System.out.println(parseScan());
        } catch (AddScanException e) {
            System.out.println("Could not process Scan " + ID + ". " + e.getError());
        }

    }

    protected void addScanBase() throws AddScanException {
        Transaction tx = null;
        try {
            tx = session.beginTransaction();
            hbscan = new org.deschutter.hibernate.Scans();
            hbscan.setId(getID());
            hbscan.setScanTypeByTypeId(getScanType());
            Planets planet = getPlanet();
            hbscan.setPlanetname(planet.getPlanetname());
            hbscan.setRulername(planet.getRulername());
            hbscan.setTick(getTick());
            session.saveOrUpdate(hbscan);
            tx.commit();
        } catch (Exception e) {
            if (tx != null) tx.rollback();
            throw new AddScanException(e.getMessage());
        }

        //Queries.addScan(getID(), getTypeID(), getX(), getY(), getZ(), getTick(), getUserID());
    }

    protected String parseScan() {
        return "";
    }


    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public BigDecimal getTick() {
        return tick;
    }

    public void setTick(BigDecimal tick) {
        this.tick = tick;
    }

    public String getScanpage() {
        return scanpage;
    }

    public void setScanpage(String scanpage) {
        this.scanpage = scanpage;
    }

    public String getX() {
        return x;
    }

    public void setX(String x) {
        this.x = x;
    }

    public String getY() {
        return y;
    }

    public void setY(String y) {
        this.y = y;
    }

    public String getZ() {
        return z;
    }

    public void setZ(String z) {
        this.z = z;
    }

    public BigDecimal getUserID() {
        return userID;
    }

    public void setUserID(BigDecimal userId) {
        this.userID = userId;
    }

    public BigDecimal getTypeID() {
        return BIGDECIMAL_ZERO;
    }


    public ScanType getScanType() {
        ScanType typde = new ScanType();
        List crit = session.createCriteria(ScanType.class).add(Restrictions.eq("id", getTypeID().byteValue())).list();

        for (Object typ : crit) {
            typde = (ScanType) typ;
            System.out.println(typde.getName());
        }

        return typde;
    }

    public Planets getPlanet() {
        Planets planet = new Planets();
        Criteria crit = session.createCriteria(Planets.class);
        crit.add(Restrictions.eq("x", getX().toString()));
        crit.add(Restrictions.eq("y", getY().toString()));
        crit.add(Restrictions.eq("z", getZ().toString()));

        for (Object typ : crit.list()) {
            planet = (Planets) typ;
            System.out.println(planet.getPlanetname() + " " + planet.getRulername());
        }
        return planet;
    }

    public Scans getHbscan() {
        return hbscan;
    }

    public void setHbscan(Scans hbscan) {
        this.hbscan = hbscan;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }
public Stats getShip(String name) {
        Stats stat = new Stats();
        Criteria crit = getSession().createCriteria(Stats.class).add(Restrictions.eq("name", name));
        for (Object typ : crit.list()) {
            stat = (Stats) typ;
        }
        return stat;
    }
}
